/* 
 * GrayRabbit Cms基于J2EE架构内容管理系统
 * Copyright © GrayRabbit Co., Ltd. All rights reserved.
 * Department：运营部
 * 更多信息请访问：
 * http://code.google.com/p/gray-rabbit-cms/
 */
package org.grayrabbit.cms.dao.dictionary.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.grayrabbit.cms.dao.dictionary.DictionaryDao;
import org.grayrabbit.cms.entity.Dictionary;
import org.grayrabbit.cms.vo.dictionary.DictionaryBean;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import org.grayrabbit.dao.impl.GenericHibernateDao;
import org.grayrabbit.dao.util.Page;
import org.grayrabbit.dao.util.QueryCondition;

/**
 * <b>Dictionary</b> dao implementation 





 * @version 1.0，创建时间：2011-05-16 14:46:47 
 */
@Repository
public class DictionaryDaoImpl extends GenericHibernateDao<Dictionary, String>
		implements DictionaryDao {

	/**
	 * HQL主体语句
	 */
	public static final String MAIN_HQL = "From Dictionary as obj";
	/**
	 * HQL中count统计hql片段
	 */
	public static final String COUNT_HQL = "Select Count(obj) ";

	/**
	 * 根据bean组装查询条件
	 * @see DictionaryDao#assembly(DictionaryBean)
	 */
	public QueryCondition assembly(DictionaryBean bean) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * 根据bean组装统计查询条件
	 * @see DictionaryDao#assemblyCount(DictionaryBean)
	 */
	public QueryCondition assemblyCount(DictionaryBean bean) {
		QueryCondition qc = new QueryCondition(COUNT_HQL + MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * @see DictionaryDao#count(DictionaryBean)
	 */
	public int count(DictionaryBean bean) {
		return ((Long)iterator(assemblyCount(bean)).next()).intValue();
	}

	/**
	 * 根据bean组装查询条件(名称参数)
	 * @see DictionaryDao#assemblyNameQuery(DictionaryBean)
	 */
	public QueryCondition assermblyNameQuery(DictionaryBean bean) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		transQuery(bean, qc);
		return qc;
	}

	/**
	 * 根据bean组装统计查询条件(名称参数)
	 * @see DictionaryDao#assemblyNameQueryCount(DictionaryBean)
	 */
	public QueryCondition assemblyNameQueryCount(DictionaryBean bean) {
		QueryCondition qc = new QueryCondition(COUNT_HQL + MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * 填充分页组件
	 * @see DictionaryDao#findByPage(Page, DictionaryBean)
	 */
	public List<Dictionary> findByPage(Page<Dictionary> page,
			DictionaryBean bean) {
		QueryCondition qc = assembly(bean);
		qc.setMaxResults(page.getPageSize());
		qc.setOffset(page.getOffset());
		return findByQc(qc);
	}
	/**
	 * 根据类型ID获取字典信息
	 * @param bean
	 * @return
	 */
	public List<Dictionary> findByDicTypeId(DictionaryBean bean){
		QueryCondition qc = assermblyNameQuery(bean);
		return findByQc(qc);
	}
	/**
	 * 根据数据字典KEY获取字典对象
	 * @param dicTypeId
	 * @param dicId
	 * @return
	 */
	public List<Dictionary> findAllByOrder(){
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		qc.append(" order by obj.ord asc");
		return findByQc(qc);
	}
	
	// 业务查询主要逻辑代码
	private void transNameQuery(DictionaryBean bean,QueryCondition qc){
		if(bean!=null){
			if(bean.getDictionaryKey() != null){
				qc.add("And obj.dictionaryKey like ? ", "%"+bean.getDictionaryKey()+"%");
			}
            if(bean.getDictionaryValue() != null){
				qc.add("And obj.dictionaryValue like ? ", "%"+bean.getDictionaryValue()+"%");
			}
            if(bean.getMemo() != null){
				qc.add("And obj.memo like ? ", bean.getMemo()+"%");
			}
            if(bean.getOrd() != null){
				qc.add("And obj.ord like ? ", bean.getOrd()+"%");
			}
            if(StringUtils.isNotBlank(bean.getDictionaryType().getDicTypeId())){
				qc.add("And obj.dictionaryType.dicTypeId like ? ", bean.getDictionaryType().getDicTypeId()+"%");
			}
		}
	}
	
	// 业务查询主要逻辑代码
	private void transQuery(DictionaryBean bean,QueryCondition qc){
		if(bean!=null){
            if(StringUtils.isNotBlank(bean.getDictionaryType().getDicTypeId())){
				qc.add("And obj.dictionaryType.dicTypeId like ? ", bean.getDictionaryType().getDicTypeId());
			}
		}
	}

	public List<Dictionary> findTopLevelDicByTypeId(String dicTypeId) {
		if(StringUtils.isBlank(dicTypeId)) return null;
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		qc.add(" And obj.dictionaryType.dicTypeId = ?", dicTypeId);
		qc.append(" And obj.dictionary = '-1' " );
		qc.appenOrderBy(" obj.ord asc");
		return findByQc(qc);
	}
	public Dictionary findDicByCode(String key) {
		if(StringUtils.isBlank(key)) return null;
		Criteria criteria =  getSession().createCriteria(Dictionary.class);
		criteria.add(Restrictions.eq("dictionaryKey", key)); 
		return (Dictionary) criteria.uniqueResult();
	}
	public boolean findByParentId(String parentid){
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		qc.add(" And obj.dictionary.dictionaryId = ?", parentid);
		if(findByQc(qc).isEmpty()){
			return false;
		}else{
			return true;
		}
		
	}
	public void updateDictionary(Dictionary d2){
		this.merge(d2);
	}

	public String findByValue(String dictionaryValue) {
		String dkey = "";
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		qc.add(" and obj.dictionaryValue = ?", dictionaryValue);
		List<Dictionary> li = findByQc(qc);
		if(li.size() > 0){
			Dictionary d = li.get(0);
			dkey = d.getDictionaryKey();
		}
		return dkey;
	}
}